package com.fingard.dsp.bank.directbank.boc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOC01Deposit extends BOC01Base implements IDeposit {
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0130";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0130-rq");
            xmlWriter.writeStartElement("b2e0130-rq");
            xmlWriter.writeStartElement("account");
            String areaCode = getBankConfig().getMapKeyByValue("areaMapping", p_req7507.actArea);
            areaCode = StringHelper.hasAnyChar(areaCode) ? areaCode : p_req7507.actArea;
            xmlWriter.writeElementString("ibknum", areaCode);
            xmlWriter.writeElementString("actacn", p_req7507.actNum);
            xmlWriter.writeEndElement();
            xmlWriter.writeElementString("type", "0");
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0130-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0130-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        while (tmpBkReader.read("/bocb2e/trans/trn-b2e0130-rs/b2e0130-rs")) {
                            XElement readXElement = tmpBkReader.readedXElement;
                            RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                            retAtsDetail.actNum = p_req7507.actNum;//银行账号
                            retAtsDetail.actArea = p_req7507.actArea;//账号地区区域码
                            retAtsDetail.depositCode = readXElement.getSingleTextNotNull("saveno");//存单号
                            retAtsDetail.currencyID = convertCurToATS(readXElement.getSingleTextNotNull("trncur"));//币种
                            String terms = "9";
                            String bankTerm = readXElement.getSingleTextNotNull("term");
                            String savingtype = readXElement.getSingleTextNotNull("savingtype");//存款类型 定期存款：202；通知存款：82、166
                            if (savingtype.equals("202")) {
                                if (bankTerm.equals("1")) {
                                    terms = "2";
                                } else if (bankTerm.equals("3")) {
                                    terms = "3";
                                } else if (bankTerm.equals("6")) {
                                    terms = "4";
                                } else if (bankTerm.equals("12")) {
                                    terms = "5";
                                } else if (bankTerm.equals("24")) {
                                    terms = "6";
                                } else if (bankTerm.equals("36")) {
                                    terms = "7";
                                } else if (bankTerm.equals("60")) {
                                    terms = "8";
                                }
                            } else if (savingtype.equals("82") || savingtype.equals("166")) {
                                if (bankTerm.equals("1")) {
                                    terms = "0";
                                } else if (bankTerm.equals("7")) {
                                    terms = "1";
                                }
                            }
                            retAtsDetail.terms = terms;
                            if (readXElement.getSingleTextNotNull("valuedate").length() == 8) {
                                retAtsDetail.startDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("valuedate")));//起息日
                                retAtsDetail.valueDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("valuedate")));//起息日
                            }
                            if (readXElement.getSingleTextNotNull("duedate").length() == 8) {
                                retAtsDetail.endDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("duedate")));//起息日
                            }
                            retAtsDetail.rate = readXElement.getSingleTextNotNull("rate").replace("%", "");
                            retAtsDetail.noteMoney = readXElement.getSingleTextNotNull("txnamt");
                            retAtsDetail.depositStates = "1";
                            String duetype = readXElement.getSingleTextNotNull("duetype");//到期处理方式 0：手工支取 1：本息自动转存 2：自动支取 3:本金自动转存
                            if (duetype.equals("1")) {
                                retAtsDetail.dumpFlag = "2";
                            } else if (duetype.equals("3")) {
                                retAtsDetail.dumpFlag = "3";
                            } else {
                                retAtsDetail.dumpFlag = "1";
                            }
                            retAts.writeRetDetail(retAtsDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                            tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                        }
                        tmpRespInfo = tmprspmsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
